package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.index.LindiIndexQueryVO;
import com.minimalist.common.entity.lindi.vo.index.LindiIndexVO;
import com.minimalist.common.feign.client.lindi.LindiIndexFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * 林地指标 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class LindiIndexFeignClientFallbackFactory extends BaseFallbackFactory<LindiIndexFeignClient> {
    private static final Logger log = LoggerFactory.getLogger(LindiIndexFeignClientFallbackFactory.class);


    @Override
    public LindiIndexFeignClient create(Throwable cause) {
        return new LindiIndexFeignClient() {
            @Override
            public R<?> addIndex(LindiIndexVO indexVO) {
                logServiceUnavailable("添加指标", indexVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "添加指标失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteIndexByIndexId(Long indexId) {
                logServiceUnavailable("删除指标", indexId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "删除指标失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateIndexByIndexId(LindiIndexVO indexVO) {
                logServiceUnavailable("修改指标", indexVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "修改指标失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<LindiIndexVO>> getPageIndexList(LindiIndexQueryVO queryVO) {
                logServiceUnavailable("分页查询指标列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "分页查询指标列表失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiIndexVO> getIndexByIndexId(Long indexId) {
                logServiceUnavailable("获取指标详情", indexId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "获取指标详情失败，服务不可用: " + cause.getMessage());
            }
            
            @Override
            public R<Map<String, Object>> getStatistics() {
                logServiceUnavailable("获取市储备库指标统计数据", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "获取市储备库指标统计数据失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<java.util.List<String>> getEnableTradeTownNameList() {
                logServiceUnavailable("获取可交易的镇街名称列表", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "获取可交易的镇街名称列表失败，服务不可用: " + cause.getMessage());
            }
        };
    }
}

